package org.itboys.rpc.service;

import org.itboys.commons.utils.number.ToNumberUtils;
import org.itboys.commons.utils.servlet.ServletContextHolder;
import org.itboys.framework.rpc.BaseRpc;
import org.itboys.framework.rpc.LocalServletHolder;
import org.itboys.rpc.constant.RpcConstant;
import org.springframework.beans.factory.annotation.Autowired;

public class BaseRpcService extends BaseRpc{

	@Autowired
	protected LocalServletHolder localServletHolder;
	
	protected Long getMemberId() {
		return getLongValueFromSession(RpcConstant.MEMBER_SESSION_KEY);
	}

	protected Long getLongValueFromSession(String sessionKey) {
		return ToNumberUtils.getLongValue(getValueFromSession(sessionKey));
	}

	protected String getStringValueFromSession(String sessionKey) {
		Object value =  getValueFromSession(sessionKey);
		return value == null ? null : value.toString();
	}

	protected int getIntValueFromSession(String sessionKey) {
		return ToNumberUtils.getIntegerValue(getValueFromSession(sessionKey));
	}

	protected <T> boolean addToSession(String sessionKey, T value) {
		return localServletHolder.addToSession(ServletContextHolder.getRequest(), ServletContextHolder.getResponse(), sessionKey, value);
	}

	protected Object getValueFromSession(String sessionKey) {
		return localServletHolder.getValueFromSession(ServletContextHolder.getRequest(), ServletContextHolder.getResponse(), sessionKey);
	}
}
